package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.Manifest;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.file.transfer.core.TfContactInfo;
import com.stark.file.transfer.core.Transferable;
import com.stark.file.transfer.core.TransferableSendManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.ContactsAdapter;
import flc.ast.adapter.LetterAdapter;
import flc.ast.bean.ApkBean;
import flc.ast.bean.MyLetterBean;
import flc.ast.databinding.ActivityContactsTransferBinding;
import flc.ast.utils.ChToEnUtils;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.bean.ContactInfo;
import stark.common.basic.event.usersys.UserSysEventProxy;
import stark.common.basic.utils.ContactUtil;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class ContactsTransferActivity extends BaseAc<ActivityContactsTransferBinding> {

    private ContactsAdapter contactsAdapter;
    private List<ContactInfo> listSel = new ArrayList<>();
    private boolean isSelectAll = false;
    private LetterAdapter letterAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_contacts_transfer;
    }

    @Override
    protected void initView() {
        mDataBinding.ivContactsTransferBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        //联系人
        mDataBinding.rvContactsList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        contactsAdapter = new ContactsAdapter();
        mDataBinding.rvContactsList.setAdapter(contactsAdapter);
        contactsAdapter.setOnItemClickListener(this);
        //字母索引
        mDataBinding.rvLetterList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        letterAdapter = new LetterAdapter();
        mDataBinding.rvLetterList.setAdapter(letterAdapter);
        letterAdapter.setOnItemClickListener(this);

        mDataBinding.llSelectAll.setOnClickListener(this);
        mDataBinding.tvStartTransfer.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llSelectAll:
                listSel.clear();
                if (isSelectAll) {
                    for (ContactInfo validDatum : contactsAdapter.getValidData()) {
                        validDatum.setSelected(false);
                    }
                } else {
                    for (ContactInfo validDatum : contactsAdapter.getValidData()) {
                        validDatum.setSelected(true);
                        listSel.add(validDatum);
                    }
                }
                isSelectAll();
                contactsAdapter.notifyDataSetChanged();
                break;
            case R.id.tvStartTransfer:
                if (listSel.size()==0){
                    return;
                }
                if (!UserSysEventProxy.getInstance().isVip() && listSel.size() > 5) {
                    ToastUtils.showShort(getString(R.string.sel_contacts_not_vip));
                    return;
                }
                List<Transferable> transferableList = new ArrayList<>();
                TfContactInfo tfContactInfo = new TfContactInfo(listSel);
                transferableList.add(tfContactInfo);
                TransferableSendManager.getInstance().setTransferables(transferableList);
                startActivity(FileSendShowQrActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (contactsAdapter.getItem(position).isSelected()) {
            contactsAdapter.getItem(position).setSelected(false);
            listSel.remove(contactsAdapter.getItem(position));
            contactsAdapter.notifyDataSetChanged();
        } else {
            listSel.add(contactsAdapter.getItem(position));
            contactsAdapter.getItem(position).setSelected(true);
            contactsAdapter.notifyDataSetChanged();
        }
        isSelectAll();
    }

    @Override
    protected void initData() {
        List<MyLetterBean> listLetter = new ArrayList<>();
        for (String letter : Content.Letter) {
            listLetter.add(new MyLetterBean(letter, false));
        }
        listLetter.get(0).setSelect(true);
        letterAdapter.setList(listLetter);
        mDataBinding.tvSelNum.setText(getString(R.string.sel_num, 0));
        getPermission();
    }

    //获取权限
    private void getPermission() {
        StkPermissionHelper.permission(Manifest.permission.READ_CONTACTS
                        , Manifest.permission.WRITE_CONTACTS)
                .reqPermissionDesc(getString(R.string.get_contacts_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }

                    @Override
                    public void onDenied(boolean needGoSettings) {
                        mDataBinding.rvLetterList.setVisibility(View.GONE);
                        mDataBinding.ivContactsTransferNoData.setVisibility(View.VISIBLE);
                    }
                }).request();
    }

    //获取通讯录
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<ContactInfo>>() {
            @SuppressLint("MissingPermission")
            @Override
            public void doBackground(ObservableEmitter<List<ContactInfo>> emitter) {
                List<ContactInfo> contactInfoList = ContactUtil.getAllContact();
                //首字母排序
                Collections.sort(contactInfoList, new Comparator<ContactInfo>() {
                    @Override
                    public int compare(ContactInfo e1, ContactInfo e2) {
                        String firstName1 = e1.getName().charAt(0) + "";
                        String spells1 = ChToEnUtils.getSpells(firstName1);
                        String firstName2 = e2.getName().charAt(0) + "";
                        String spells2 = ChToEnUtils.getSpells(firstName2);
                        return spells1.compareTo(spells2);
                    }
                });
                emitter.onNext(contactInfoList);
            }

            @Override
            public void accept(List<ContactInfo> list) {
                if (list != null && list.size() > 0) {
                    contactsAdapter.setList(list);
                    mDataBinding.rvContactsList.setVisibility(View.VISIBLE);
                    mDataBinding.rvLetterList.setVisibility(View.VISIBLE);
                    mDataBinding.ivContactsTransferNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvContactsList.setVisibility(View.GONE);
                    mDataBinding.rvLetterList.setVisibility(View.GONE);
                    mDataBinding.ivContactsTransferNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    private void isSelectAll() {
        mDataBinding.tvSelNum.setText(getString(R.string.sel_num, listSel.size()));
        if (listSel.size() == contactsAdapter.getValidData().size()) {
            mDataBinding.ivSelectAll.setImageResource(R.drawable.ayiquanx);
            isSelectAll = true;
        } else {
            mDataBinding.ivSelectAll.setImageResource(R.drawable.aquanx);
            isSelectAll = false;
        }
    }

}